﻿using Abp.Domain.Repositories;
using MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class PhaseInfoAppService : AbpZeroTemplateAppServiceBase, IPhaseInfoAppService
    {
        private IRepository<PhaseInfo> phaseInfoRepository;
        private IRepository<PhaseItem> phaseItemRepository;

        public PhaseInfoAppService(IRepository<PhaseInfo> _phaseInfoRepository, IRepository<PhaseItem> _phaseItemRepository)
        {
            phaseInfoRepository = _phaseInfoRepository;
            phaseItemRepository = _phaseItemRepository;
        }

        public List<Dto.GetPhaseInfoTreeOutPut> GetAll()
        {
            List<Dto.GetPhaseInfoTreeOutPut> outPut = new List<Dto.GetPhaseInfoTreeOutPut>();
            var phases = phaseInfoRepository.GetAll().OrderBy(x => x.Indexno).ToList();
            var child = phaseItemRepository.GetAll().OrderBy(x => x.Indexno).ToList();
            foreach (var item in phases)
            {
                var id = (item.Id * -1).ToString();
                var parent = new Dto.GetPhaseInfoTreeOutPut()
                {
                    id = id,
                    leaf = !child.Any(m => m.PhaseId == item.Id),
                    expanded = true,
                    isEnabled = item.IsEnabled,
                    parentId = "0",
                    text = item.Name,
                    Indexno = item.Indexno,
                    children = new List<Dto.GetPhaseInfoTreeOutPut>()
                };
                var children = child.Where(x => x.PhaseId == item.Id).OrderBy(x => x.Indexno);
                foreach (var c in children)
                {
                    var p = new Dto.GetPhaseInfoTreeOutPut()
                    {
                        id = c.Id.ToString(),
                        leaf = true,
                        Indexno = c.Indexno,
                        expanded = true,
                        isEnabled = c.IsEnabled,
                        parentId = id,
                        text = c.Name,
                        children = new List<Dto.GetPhaseInfoTreeOutPut>()
                    };
                    parent.children.Add(p);
                }
                outPut.Add(parent);
            }
            return outPut;
        }


        public List<GetPhaseInfoOutPut> GetPhaseInfo()
        {
            var result = phaseInfoRepository.GetAll().OrderBy(x => x.Indexno).Where(x => x.IsEnabled).Select(x => new GetPhaseInfoOutPut()
            {
                Id = x.Id,
                Name = x.Name,
                PhaseItems = x.PhaseItems.Where(m => m.IsEnabled).Select(q =>
                    new GetPhaseItemOutPut()
                {
                    Id = q.Id,
                    Name = q.Name
                }).ToList()
            }).ToList();
            return result;
        }


        public async Task CreatePhase(CreatePhaseInPut input)
        {
            List<string> details = input.Detail.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var phaseId = await phaseInfoRepository.InsertAndGetIdAsync(new PhaseInfo()
            {
                IsEnabled = true,
                Indexno = phaseInfoRepository.GetAll().Max(x => x.Indexno) + 1,
                Name = input.Name,
                ItemCount = details.Count
            });
            foreach (var item in details)
            {
                await phaseItemRepository.InsertAsync(new PhaseItem()
                {
                    Indexno = phaseItemRepository.GetAll().Max(x => x.Indexno) + 1,
                    IsEnabled = true,
                    Name = item,
                    PhaseId = phaseId
                });
            }
        }


        public async Task CreatePhaseItem(CreatePhaseItemInPut input)
        {
            List<string> details = input.Detail.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var item in details)
            {
                await phaseItemRepository.InsertAsync(new PhaseItem()
                {
                    Indexno = phaseItemRepository.GetAll().Max(x => x.Indexno) + 1,
                    IsEnabled = true,
                    Name = item,
                    PhaseId = input.PhaseId
                });
            }
            var phase = await phaseInfoRepository.GetAsync(input.PhaseId);
            phase.ItemCount = phaseItemRepository.GetAll().Count();
        }


        /// <summary>
        /// 编辑阶段或详细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task EditPhase(List<EditPhaseInPut> input)
        {
            foreach (var item in input)
            {
                if (item.Id < 0)
                {
                    var phase = await phaseInfoRepository.GetAsync(item.Id * -1);
                    phase.Name = item.Name;
                    phase.Indexno = item.Indexno;
                    phase.IsEnabled = item.IsEnabled;
                }
                else
                {
                    var phaseItem = await phaseItemRepository.GetAsync(item.Id);
                    phaseItem.Name = item.Name;
                    phaseItem.Indexno = item.Indexno;
                    phaseItem.IsEnabled = item.IsEnabled;
                }
            }
        }


        public async Task Delete(int id)
        {
            if (id > 0)
            {
                await phaseItemRepository.DeleteAsync(id);
            }
            else
            {
                id = Math.Abs(id);
                await phaseInfoRepository.DeleteAsync(id);
                await phaseItemRepository.DeleteAsync(x => x.PhaseId == id);
            }
        }
    }
}
